<template>
  <div class="mypanel">
    <el-dialog title="新增员工" :visible="dialogShow" @close="cancle">
      <el-form ref="myform" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile"></el-input
        ></el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            >
            </el-option> </el-select
        ></el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input
        ></el-form-item>
        <el-form-item class="myformitem" label="部门" prop="departmentName">
          <el-input v-model="form.departmentName" @focus="myfocus"></el-input>
          <el-tree
            class="mytree"
            v-if="showTree"
            :data="depList"
            :props="{ label: 'name' }"
            default-expand-all
            :expand-on-click-node="false"
            @node-click="nodeClick"
          ></el-tree>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button @click="submit" type="primary">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入枚举数据
import myData from '@/api/constant/employees'
import { getDeptApi } from '@/api/departments'
import { addEmpApi } from '@/api/employees'
export default {
  props: ['dialogShow'],
  data () {
    return {
      // 控制树状结构的显示与隐藏
      showTree: false,
      // 保存聘用形式的列表
      hireType: myData.hireType,
      depList: [
        { label: '北京', children: [{ label: '朝阳' }] },
        { label: '深圳' }
      ], // 部门列表
      // 数据源
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      // 添加规则
      rules: {
        username: [{ required: true, message: '不能为空', trigger: 'blur' }], // 姓名
        mobile: [{ required: true, message: '不能为空', trigger: 'blur' }], // 手机号
        timeOfEntry: [{ required: true, message: '不能为空', trigger: 'blur' }], // 入职时间
        formOfEmployment: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ], // 聘用形式
        workNumber: [{ required: true, message: '不能为空', trigger: 'blur' }], // 工号
        departmentName: [
          { required: true, message: '不能为空', trigger: 'change' }
        ], // 部门
        correctionTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ] // 转正时间
      }
    }
  },
  methods: {
    // 提交新增的数据
    submit () {
      // 全局验证
      this.$refs.myform.validate(async res => {
        if (res) {
          // 验证通过
          await addEmpApi(this.form)
          // 关闭面板
          this.$emit('update:dialogShow', false)
          // 清空数据
          this.form = {
            username: '', // 姓名
            mobile: '', // 手机号
            timeOfEntry: '', // 入职时间
            formOfEmployment: '', // 聘用形式
            workNumber: '', // 工号
            departmentName: '', // 部门
            correctionTime: '' // 转正时间
          }
          // 提示成功
          this.$message({
            type: 'success',
            message:'新增成功'
          })
          // 重新加载数据
          this.$emit('updateData')
        } else {
          // 不通过
        }
      })
    },
    // 得到所有的部门数据
    async getDept () {
      const res = await getDeptApi()
      this.depList = this.trunData(res.data.depts, '')
    },
    // 将数据转换的嵌套结构
    trunData (arr, pid) {
      const res = []
      arr.forEach(item => {
        if (item.pid === pid) {
          const son = this.trunData(arr, item.id)
          if (son.length > 0) {
            item.children = son
          }
          res.push(item)
        }
      })
      return res
    },
    // 点击树状节点的元素时会触发
    nodeClick (data) {
      // 将选中的文本赋值给 form 中的部门名称
      this.form.departmentName = data.name
      // 关闭面板
      this.showTree = false
    },
    myfocus () {
      // 显示 tree
      this.showTree = true
    },
    cancle () {
      // 关闭面板
      this.$emit('update:dialogShow', false)
    }
  },
  created () {
    this.getDept()
  }
}
</script>

<style lang="scss">
.mypanel {
  .myformitem {
    position: relative;
    width: 400px;
    .mytree {
      position: absolute;
      top: 50px;
      left: 0;
      z-index: 999;
      border: 1px solid #ccc;
      width: 100%;
      border-radius: 8px;
      .el-tree-node__content {
        height: 40px;
      }
    }
  }
}
</style>
